﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sispe.Entities;

namespace Sispe.Repository
{
    public class GradoRepository:MasterRepository, IGradoRepository
    {
        public List<GRADO> GetAllFromGrados()
        {
            var query = from p in context.GRADOes
                        select p;

            return query.ToList();
        }

        public List<GRADO> GetAllfromGradosByDescripcion(string criterio)
        {
            if (criterio != string.Empty)
            {
                var query = from p in context.GRADOes
                            where p.Descripcion.ToUpper().Contains(criterio.ToUpper())
                            select p;
                return query.ToList();
            }
            else
            {
                var query = from p in context.GRADOes
                            select p;

                return query.ToList();
            }
        }

        public GRADO GetFromGradosById(int id)
        {
            var query = from p in context.GRADOes
                        where p.IdGrado.Equals(id)
                        select p;

            return query.SingleOrDefault();
        }

        public void AddGrados(GRADO grado)
        {
            grado.ESTUDIOS = null;
            context.GRADOes.Add(grado);
            try
            {
                context.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException e)
            {

                var outputLines = new List<string>();
                foreach (var eve in e.EntityValidationErrors)
                {
                    outputLines.Add(string.Format(
                        "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:",
                        DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        outputLines.Add(string.Format(
                            "- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage));
                    }
                }
                if (System.IO.File.Exists(@"D:\errors.txt"))
                {
                    System.IO.File.Delete(@"D:\errors.txt");
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                else
                {
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                throw;
            }
        }

        public void UpdateGrados(GRADO grado)
        {
            grado.ESTUDIOS = null;
            context.Entry(grado).State = System.Data.EntityState.Modified;
            try
            {
                context.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException e)
            {

                var outputLines = new List<string>();
                foreach (var eve in e.EntityValidationErrors)
                {
                    outputLines.Add(string.Format(
                        "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:",
                        DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        outputLines.Add(string.Format(
                            "- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage));
                    }
                }
                if (System.IO.File.Exists(@"D:\errors.txt"))
                {
                    System.IO.File.Delete(@"D:\errors.txt");
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                else
                {
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                throw;
            }
        }

        public void DeleteGrados(int id)
        {
            var existe = context.GRADOes.Find(id);

            if (existe != null)
            {
                context.GRADOes.Remove(existe);
                context.SaveChanges();
            }
        }

        public bool ExisteDUplicidad(string criterio, int id)
        {
            var query = from p in context.GRADOes
                        where p.Descripcion.ToUpper().CompareTo(criterio.ToUpper()) == 0 && p.IdGrado != id
                        select p;

            return query.Any();
        }
    }
}
